package com.winspread.ui.squareview.base

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.annotation.Nullable

/**
 *Author: Amin
 *Data: 2022/4/11 10:21
 *
 *Description:  控件<selector>元素的 XML 文件中定义
 *
 */
class SquareView(
    @Nullable context: Context?,
    @Nullable attrs: AttributeSet?,
    defStyleAttr: Int,
    private val view: View,
) {

    private var squareNormalView: SquareNormalView = SquareNormalView(context, attrs)

    var squareFocusedView: SquareAssistantView = SquareAssistantView(context, attrs, squareNormalView)
    var squarePressedView: SquareAssistantView = SquareAssistantView(context, attrs, squareNormalView)

    init {
        refreshView()
    }

    fun refreshView() {
        view.background = getBackGround()
        if (view is TextView) {
            if (squareNormalView.textColor != 0) {
                view.setTextColor(getColor())
            }
            view.setCompoundDrawables(
                getDraw("left"),
                getDraw("top"),
                getDraw("right"),
                getDraw("bottom")
            )

        }
    }

    /**
     * 设置图片的左右位置（选中，点击等变化）
     *
     * @param type String
     * @return Drawable?
     */
    private fun getDraw(type: String): Drawable? {
        val draw = StateListDrawable()
        var normal: Drawable? = squareNormalView.leftDraw
        var pressed: Drawable? = squarePressedView.leftDraw
        var focused: Drawable? = squareFocusedView.leftDraw

        when (type) {
            "top" -> {
                normal = squareNormalView.topDraw
                pressed = squarePressedView.topDraw
                focused = squareFocusedView.topDraw
            }
            "bottom" -> {
                normal = squareNormalView.bottomDraw
                pressed = squarePressedView.bottomDraw
                focused = squareFocusedView.bottomDraw
            }
            "right" -> {
                normal = squareNormalView.rightDraw
                pressed = squarePressedView.rightDraw
                focused = squareFocusedView.rightDraw
            }
        }

        if (normal == null) {
            return null
        }
        if (pressed == null) {
            pressed = normal
        }
        if (focused == null) {
            focused = normal
        }

        draw.addState(intArrayOf(android.R.attr.state_pressed), pressed)
        draw.addState(intArrayOf(android.R.attr.state_focused), focused)
        draw.addState(intArrayOf(android.R.attr.state_selected), focused)
        draw.addState(intArrayOf(android.R.attr.state_checked), focused)
        draw.addState(intArrayOf(), normal)
        draw?.setBounds(0, 0, draw.minimumWidth, draw.minimumHeight);
        return draw

    }


    /**
     * 设置背景性质（选中，点击等变化）
     *
     * @return StateListDrawable
     */
    private fun getBackGround(): StateListDrawable {
        val bg = StateListDrawable()
        val normal: Drawable = squareNormalView.getBackGround()
        val pressed: Drawable = squarePressedView.getBackGround()
        val focused: Drawable = squareFocusedView.getBackGround()
        bg.addState(intArrayOf(android.R.attr.state_pressed), pressed)
        bg.addState(intArrayOf(android.R.attr.state_focused), focused)
        bg.addState(intArrayOf(android.R.attr.state_selected), focused)
        bg.addState(intArrayOf(android.R.attr.state_checked), focused)
        bg.addState(intArrayOf(), normal)
        return bg
    }

    /**
     * 设置文字颜色（选中，点击等变化）
     *
     * @return ColorStateList
     */
    private fun getColor(): ColorStateList {
        val colors = intArrayOf(
            squarePressedView.textColor!!,
            squareFocusedView.textColor!!,
            squareFocusedView.textColor!!,
            squareFocusedView.textColor!!,
            squareNormalView.textColor!!
        )
        val states = arrayOfNulls<IntArray>(5)
        states[0] = intArrayOf(android.R.attr.state_pressed)
        states[1] = intArrayOf(android.R.attr.state_focused)
        states[2] = intArrayOf(android.R.attr.state_selected)
        states[3] = intArrayOf(android.R.attr.state_checked)
        states[4] = intArrayOf()
        return ColorStateList(states, colors)
    }

    /**
     * 正常时候的弧度
     * 左上 左下 右上 右下
     * 单位是px
     */
    fun setCorners(float: FloatArray) {
        squareNormalView.apply {
            cornerLt = float[0]
            cornerLb = float[1]
            cornerRt = float[2]
            cornerRb = float[3]
        }
        view.background = getBackGround()
    }

    /**
     * 获取焦点
     * 左上 左下 右上 右下
     * 单位是px
     */
    fun setCornersFocused(float: FloatArray) {
        squareFocusedView.apply {
            cornerLt = float[0]
            cornerLb = float[1]
            cornerRt = float[2]
            cornerRb = float[3]
        }
        view.background = getBackGround()
    }

    /**
     * 按下
     * 左上 左下 右上 右下
     * 单位是px
     */
    fun setCornersPressed(float: FloatArray) {
        squarePressedView.apply {
            cornerLt = float[0]
            cornerLb = float[1]
            cornerRt = float[2]
            cornerRb = float[3]
        }
        view.background = getBackGround()
    }


}